package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.IComicDataDAL;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.impl.ComicReaderFinishViewModel;
import com.qire.manhua.model.vo.CollectTaskVO;
import com.qire.manhua.model.vo.ComicReadFinishPushVO;

public class ComicReaderFinishService extends DataService {

    private final IComicDataDAL comicDataDAL = WarehouseFactory.create(this, IComicDataDAL.class);

    private final ComicReaderFinishViewModel viewModel;

    public ComicReaderFinishService(ComicReaderFinishViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 获取漫画阅读结束推送信息
     * @param comicId
     */
    public void fetchComicReadFinishPush(int comicId) {
        comicDataDAL.fetchComicReadFinishPush(comicId);
    }
    @StrategyAnnotation(event = Url.ComicReadFinishPush)
    public void fetchComicReadFinishPushCallback(ComicReadFinishPushVO dataVO, boolean isCache) {
        if(ObjectUtil.isNull(dataVO)) {
            return;
        }

        String  bookName            = "";
        String  pushBookTitle       = "";
        String  chapterName         = "";
        int     lastChapterNumber   = 0;
        String  nextUpdateDate      = "";
        String  coverImage          = "";
        int     likeCount           = 0;
        boolean isFinish            = false;

        if(ObjectUtil.nonNull(dataVO.srcBookInfo)) {
            bookName        = dataVO.srcBookInfo.book_name;
            nextUpdateDate  = dataVO.srcBookInfo.update_cycle;
            isFinish        = dataVO.srcBookInfo.end_state == 1;
        }

        if(ObjectUtil.nonNull(dataVO.bookInfo) && ObjectUtil.nonNull(dataVO.chapterInfo)) {
            pushBookTitle       = dataVO.bookInfo.book_name;
            lastChapterNumber   = dataVO.bookInfo.chapter_px;
            coverImage          = dataVO.bookInfo.book_cover;
            likeCount           = dataVO.bookInfo.tj.like_cnt;
            chapterName         = dataVO.chapterInfo.chapter_name;

            ComicBookEntity comicBookEntity = dataVO.bookInfo.toComicBookEntity();
            comicBookEntity.whetherCollect(dataVO.bookInfo.shelf == 1);

            dataVO.chapterInfo.fillComic(comicBookEntity);
            viewModel.setNextChaptersId(dataVO.chapterInfo.next_id);

            viewModel.loadComicBookEntity(comicBookEntity);
        }

        viewModel.loadComicInfo(bookName, pushBookTitle, chapterName, lastChapterNumber, nextUpdateDate, coverImage, likeCount, isFinish);
    }

    /**
     * 切换收藏状态通过漫画ID
     * @param comicId 漫画ID
     */
    public void toggleCollectionBy(int comicId) {
        comicDataDAL.whetherJoinMyBookshelf(comicId, 0);
    }
    @StrategyAnnotation(event = Url.whetherJoinMyBookshelf)
    public void whetherJoinMyBookshelfCallback(CollectTaskVO dataVO, boolean isCache) {
        if(ObjectUtil.isNull(dataVO) || dataVO.isFail()) {
            viewModel.refreshFavorite("收藏失败", "");
            return;
        }

        String resultMsg = dataVO.getResultMsg();
        String takeMsg = dataVO.getTaskMsg();
        viewModel.refreshFavorite(resultMsg, takeMsg);
    }
}
